package com.news.servlet.admin;

import com.news.service.UserService;
import com.news.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/userDelete")
public class UserDeleteServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 获取用户ID
            int userId = Integer.parseInt(request.getParameter("id"));
            
            // 检查是否是管理员账号
            if (userId == 1) {
                request.getSession().setAttribute("message", "不能删除超级管理员账号！");
                response.sendRedirect(request.getContextPath() + "/admin/userManage");
                return;
            }
            
            // 删除用户
            if (userService.deleteUser(userId)) {
                request.getSession().setAttribute("message", "用户删除成功！");
            } else {
                request.getSession().setAttribute("message", "用户删除失败！");
            }
        } catch (Exception e) {
            request.getSession().setAttribute("message", "用户删除失败：" + e.getMessage());
        }
        
        // 重定向回用户管理页面
        response.sendRedirect(request.getContextPath() + "/admin/userManage");
    }
} 